﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.App_Code;
using QXBook.Utils;
using QXBook.Model;
using System.Text;
using QXBook.Service;

namespace Web.SysManage
{
    public partial class Menu : AdminBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //检测管理员登录
            ServiceFactory.CreateInstance<AdminService>().CheckAdminLogin();
            ServiceFactory.CreateInstance<AdminService>().CheckAdminPop(-2);

            if (!Page.IsPostBack)
            {
                this.meunInit();
                this.temp2.Text = "";
            }
        }

        private void meunInit()
        {
            string tempClass = this.temp1.Text;
            string tempSClass = this.temp2.Text;

            int ParentId = CommonUtils.ToInt(CommonUtils.Get("ParendId"));
            if (ParentId == 0) ParentId = 1;


            ServiceFactory.CreateInstance<AdminService>().CheckAdminPop(ParentId);

            //获取管理员权限
            Dictionary<int, PopDomInfo> cpop = ServiceFactory.CreateInstance<AdminRuleService>().GetChildManagePopedomEntity(ParentId);

            StringBuilder sb = new StringBuilder();
            StringBuilder script = new StringBuilder();

            script.Append("<script type=\"text/javascript\">\r\n");
            script.Append("var btNum =" + cpop.Count.ToString() + ";\r\n");

            int i = 0;

            ////如果是资源管理，显示详细资源分类
            //if (ParentId == 18)
            //{
            //    //加载2级资讯分类
            //    Dictionary<string, ModelBase> categries = base.handlerService.skinService.categoriesHandlers.GetCategoriesEntityByParentId("0", ConfigServiceEx.DefaultSkinId);
            //    if (categries != null && categries.Count > 0)
            //    {
            //        foreach (KeyValuePair<string, EntityBase> keyvalue in categries)
            //        {
            //            Categories categorie = (Categories)keyvalue.Value;
            //            if (0 == i)
            //            {
            //                script.Append("window.parent.main.location.href='resources/resourceslist.aspx?skinid=" + ConfigServiceEx.DefaultSkinId
            //                    + "&iclassid=" + categorie.Id + "';\r\n");
            //            }

            //            sb.Append(string.Format(tempClass, i,
            //                "resources/resourceslist.aspx?skinid=" + ConfigServiceEx.DefaultSkinId + "&iclassid=" + categorie.Id
            //                , categorie.vcClassName));

            //            Dictionary<string, EntityBase> categries1 = base.handlerService.skinService.categoriesHandlers.GetCategoriesEntityByParentId(categorie.Id, ConfigServiceEx.DefaultSkinId);
            //            if (categries1 != null && categries1.Count > 0)
            //            {
            //                script.Append("stNums[" + i.ToString() + "]=" + categries1.Count.ToString() + ";\r\n");
            //                int m = 0;
            //                foreach (KeyValuePair<string, EntityBase> keyvalue1 in categries1)
            //                {
            //                    Categories categorie12 = (Categories)keyvalue1.Value;
            //                    sb.Append(string.Format(tempSClass, i, m,
            //                        "resources/resourceslist.aspx?skinid=" + ConfigServiceEx.DefaultSkinId + "&iclassid=" + categorie12.Id,
            //                        categorie12.vcClassName, 23));

            //                    m++;
            //                }
            //            }

            //            i++;
            //        }
            //    }
            //}
            //else
            //{
            foreach (KeyValuePair<int, PopDomInfo> keyvalue in cpop)
            {
                string Url = keyvalue.Value.Url;

                if (ServiceFactory.CreateInstance<AdminService>().CheckAdminPop(keyvalue.Value.PopID))
                {
                    if (0 == i) script.Append("window.parent.main.location.href='" + Url + "';\r\n");
                    sb.Append(string.Format(tempClass, i, Url, keyvalue.Value.PopName));

                    Dictionary<int, PopDomInfo> tpop = ServiceFactory.CreateInstance<AdminRuleService>().GetChildManagePopedomEntity(keyvalue.Value.PopID);
                    if (tpop == null) continue;

                    script.Append("stNums[" + i.ToString() + "]=" + tpop.Count.ToString() + ";\r\n");

                    int n = 0;
                    foreach (KeyValuePair<int, PopDomInfo> keyvalue1 in tpop)
                    {
                        //菜单不用验证权限
                        //if (ServiceFactory.CreateInstance<AdminService>().CheckAdminPop(keyvalue1.Value.PopID))
                        //{
                        string Url1 = keyvalue1.Value.Url;
                        sb.Append(string.Format(tempSClass, i, n, Url1, keyvalue1.Value.PopName, keyvalue1.Value.IconKey));
                        n++;
                        //}
                    }

                    i++;
                }
            }
            //}

            script.Append("</script>\r\n");
            this.temp1.Text = script.ToString();
            this.spMenu.Text = sb.ToString();
        }
    }
}